/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.sample.library.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import com.sample.library.service.AuthorBookLocalServiceUtil;
import com.sample.library.service.persistence.AuthorBookPK;

import java.io.Serializable;

import java.lang.reflect.Proxy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Nguyen Tuan Anh
 */
public class AuthorBookClp extends BaseModelImpl<AuthorBook>
	implements AuthorBook {
	public AuthorBookClp() {
	}

	public Class<?> getModelClass() {
		return AuthorBook.class;
	}

	public String getModelClassName() {
		return AuthorBook.class.getName();
	}

	public AuthorBookPK getPrimaryKey() {
		return new AuthorBookPK(_bookId, _authorId);
	}

	public void setPrimaryKey(AuthorBookPK primaryKey) {
		setBookId(primaryKey.bookId);
		setAuthorId(primaryKey.authorId);
	}

	public Serializable getPrimaryKeyObj() {
		return new AuthorBookPK(_bookId, _authorId);
	}

	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey((AuthorBookPK)primaryKeyObj);
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("bookId", getBookId());
		attributes.put("authorId", getAuthorId());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long bookId = (Long)attributes.get("bookId");

		if (bookId != null) {
			setBookId(bookId);
		}

		String authorId = (String)attributes.get("authorId");

		if (authorId != null) {
			setAuthorId(authorId);
		}
	}

	public long getBookId() {
		return _bookId;
	}

	public void setBookId(long bookId) {
		_bookId = bookId;
	}

	public String getAuthorId() {
		return _authorId;
	}

	public void setAuthorId(String authorId) {
		_authorId = authorId;
	}

	public BaseModel<?> getAuthorBookRemoteModel() {
		return _authorBookRemoteModel;
	}

	public void setAuthorBookRemoteModel(BaseModel<?> authorBookRemoteModel) {
		_authorBookRemoteModel = authorBookRemoteModel;
	}

	public void persist() throws SystemException {
		if (this.isNew()) {
			AuthorBookLocalServiceUtil.addAuthorBook(this);
		}
		else {
			AuthorBookLocalServiceUtil.updateAuthorBook(this);
		}
	}

	@Override
	public AuthorBook toEscapedModel() {
		return (AuthorBook)Proxy.newProxyInstance(AuthorBook.class.getClassLoader(),
			new Class[] { AuthorBook.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		AuthorBookClp clone = new AuthorBookClp();

		clone.setBookId(getBookId());
		clone.setAuthorId(getAuthorId());

		return clone;
	}

	public int compareTo(AuthorBook authorBook) {
		AuthorBookPK primaryKey = authorBook.getPrimaryKey();

		return getPrimaryKey().compareTo(primaryKey);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}

		AuthorBookClp authorBook = null;

		try {
			authorBook = (AuthorBookClp)obj;
		}
		catch (ClassCastException cce) {
			return false;
		}

		AuthorBookPK primaryKey = authorBook.getPrimaryKey();

		if (getPrimaryKey().equals(primaryKey)) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return getPrimaryKey().hashCode();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(5);

		sb.append("{bookId=");
		sb.append(getBookId());
		sb.append(", authorId=");
		sb.append(getAuthorId());
		sb.append("}");

		return sb.toString();
	}

	public String toXmlString() {
		StringBundler sb = new StringBundler(10);

		sb.append("<model><model-name>");
		sb.append("com.sample.library.model.AuthorBook");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>bookId</column-name><column-value><![CDATA[");
		sb.append(getBookId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>authorId</column-name><column-value><![CDATA[");
		sb.append(getAuthorId());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _bookId;
	private String _authorId;
	private BaseModel<?> _authorBookRemoteModel;
}